import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'index.dart';
import 'widgets/index.dart';
import '../../../common/values/index.dart';

class SplashPage extends GetView<SplashController> {
  const SplashPage({super.key});

  // 主视图构建方法
  Widget _buildView() {
    return InkStyleBackground(
      baseColor: const Color(0xFF2C3E50),
      opacity: 0.08,
      child: Stack(
        children: [
          // 水墨装饰元素
          _buildInkDecorations(),
          // 主要内容区域
          Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                _buildAppLogo(),
                const SizedBox(height: 40),
                _buildAppTitle(),
                const SizedBox(height: 16),
                _buildAppSubtitle(),
                const SizedBox(height: 60),
                _buildLoadingIndicator(),
              ],
            ),
          ),
          // 底部信息
          _buildBottomInfo(),
        ],
      ),
    );
  }

  // 构建水墨装饰元素
  Widget _buildInkDecorations() {
    return Stack(
      children: [
        // 左上角装饰
        Positioned(
          top: 80,
          left: 30,
          child: InkDecorationWidget(
            size: 60,
            color: const Color(0xFF2C3E50),
            opacity: 0.1,
          ),
        ),
        // 右上角装饰
        Positioned(
          top: 120,
          right: 50,
          child: InkDecorationWidget(
            size: 40,
            color: const Color(0xFF34495E),
            opacity: 0.08,
          ),
        ),
        // 左下角装饰
        Positioned(
          bottom: 150,
          left: 40,
          child: InkDecorationWidget(
            size: 80,
            color: const Color(0xFF2C3E50),
            opacity: 0.06,
          ),
        ),
        // 右下角装饰
        Positioned(
          bottom: 200,
          right: 20,
          child: InkDecorationWidget(
            size: 50,
            color: const Color(0xFF34495E),
            opacity: 0.09,
          ),
        ),
      ],
    );
  }

  // 构建应用Logo
  Widget _buildAppLogo() {
    return Container(
      width: 140,
      height: 140,
      decoration: BoxDecoration(
        shape: BoxShape.circle,
        color: Colors.white,
        boxShadow: [
          BoxShadow(
            color: const Color(0xFF2C3E50).withOpacity(0.3),
            blurRadius: 20,
            offset: const Offset(0, 10),
          ),
          BoxShadow(
            color: const Color(0xFF2C3E50).withOpacity(0.1),
            blurRadius: 40,
            offset: const Offset(0, 20),
          ),
        ],
      ),
      child: ClipOval(
        child: Padding(
          padding: const EdgeInsets.all(20),
          child: Image.asset(
            AssetsImages.logoPng,
            width: 100,
            height: 100,
            fit: BoxFit.contain,
            errorBuilder: (context, error, stackTrace) {
              // 如果图片加载失败，显示默认图标
              return Container(
                width: 100,
                height: 100,
                alignment: Alignment.center,
                child: const Icon(
                  Icons.music_note,
                  size: 60,
                  color: Color(0xFF2C3E50),
                ),
              );
            },
          ),
        ),
      ),
    );
  }

  // 构建应用标题
  Widget _buildAppTitle() {
    return Text(
      controller.appName,
      textAlign: TextAlign.center,
      style: const TextStyle(
        fontSize: 32,
        fontWeight: FontWeight.bold,
        color: Color(0xFF2C3E50),
        letterSpacing: 2.0,
        height: 1.2,
      ),
    );
  }
  
  // 构建应用副标题
  Widget _buildAppSubtitle() {
    return Text(
      controller.appSubtitle,
      textAlign: TextAlign.center,
      style: TextStyle(
        fontSize: 18,
        color: const Color(0xFF2C3E50).withOpacity(0.8),
        letterSpacing: 1.5,
        fontWeight: FontWeight.w500,
      ),
    );
  }


  // 构建加载指示器
  Widget _buildLoadingIndicator() {
    return Obx(() {
      if (controller.isLoading.value) {
        return Column(
          children: [
            SizedBox(
              width: 30,
              height: 30,
              child: CircularProgressIndicator(
                color: const Color(0xFF2C3E50).withOpacity(0.7),
                strokeWidth: 3,
              ),
            ),
            const SizedBox(height: 16),
            _buildProgressIndicator(),
          ],
        );
      }
      return const SizedBox.shrink();
    });
  }

  // 构建进度指示器
  Widget _buildProgressIndicator() {
    return SplashProgressIndicator(
      width: 200,
      height: 4,
      backgroundColor: const Color(0xFF2C3E50).withOpacity(0.2),
      progressColor: const Color(0xFF2C3E50).withOpacity(0.7),
      textColor: const Color(0xFF2C3E50).withOpacity(0.6),
      showPercentage: true,
    );
  }

  // 构建底部信息
  Widget _buildBottomInfo() {
    return Positioned(
      bottom: 50,
      left: 0,
      right: 0,
      child: Column(
        children: [
          _buildCopyright(),
          const SizedBox(height: 16),
          _buildStartupInfo(),
        ],
      ),
    );
  }

  // 构建版权信息
  Widget _buildCopyright() {
    return Text(
      '© 2025 中国音乐学院',
      style: TextStyle(
        fontSize: 12,
        color: const Color(0xFF2C3E50).withOpacity(0.5),
      ),
    );
  }

  // 构建启动信息
  Widget _buildStartupInfo() {
    return Obx(() {
      if (controller.isLoading.value) {
        return Text(
          controller.getStartupInfo(),
          style: TextStyle(
            fontSize: 10,
            color: Colors.white.withValues(alpha: 0.5),
          ),
        );
      }
      return const SizedBox.shrink();
    });
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<SplashController>(
      init: SplashController(),
      id: "splash",
      builder: (_) {
        return Scaffold(
          // 无AppBar，全屏显示
          body: _buildView(),
        );
      },
    );
  }
}
